(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	
	/**
	 * @class HteOS.desktop.DragDrop
	 * @singleton
	 * **/
	
	HteOS.desktop.DragDrop = {
		init: function(){
			
			this.spliter = $("<div class=\"hte-shortcut-dd-spliter\">").appendTo(HteOS.Desktop.getEl()).hide();
			var dd = this,margin = HteOS.desktop.View.margin,view = HteOS.desktop.View;
			
			var $el = HteOS.Desktop.getEl();
			$(".hte-shortcut").draggable({
				containment: $el,
				scroll : false,
				revert : false,
				helper:'clone',
				start: function(event, ui) {
					$el.addClass("hte-desktop-dragging");
					dd.spliter.show();
					//ui.draggable.dragSource = app;
				},
				stop: function(event, ui) {
					$el.removeClass("hte-desktop-dragging");
					dd.spliter.hide();
					if(dd.target){
						var source = $(this);
						view.insert(source.data("appId"),$(dd.target.target).data("appId"),dd.target.position)
						/*var source = $(this);
						if(dd.target.action == 'before'){
							source.insertBefore(dd.target.target);
						}else{
							source.insertAfter(dd.target.target)
						}*/
						dd.target = null;
					}
				}
			});
			
			$(".hte-shortcut").droppable({
				accept: ".hte-shortcut",
				over: function(event, ui) {
					var source = $(ui.draggable.get(0));
					var pageY = event.pageY;
					var target = $(this),pos =  target.position();
					var baseLine = pos.top + (target.height() / 2);
					if(pageY < baseLine){
						dd.spliter.css({
							top : pos.top - margin / 2,
							left : pos.left
						});
						dd.target = {
							target : target,
							position : 'before'
						};
					}else{
						dd.spliter.css({
							top : pos.top + target.height() + margin / 2,
							left : pos.left
						});
						dd.target = {
							target : target,
							position : 'after'
						};
					}
				}
			});
		}
	};
}));